package my_arrayList;/*
 *@ 代阳敲的专属代码
 **/

import Demo1.tree.BinaryTree;

import java.util.Arrays;
import java.util.List;

public class MyArrayList <E> implements IList ,Comparable<E>{
    private int[]element=new int [3];
    private int usSize=0;


    @Override
    public void add(int data) {
        if(isFull()) {
            element=dilatation();
        }
        element[usSize]=data;
        usSize++;
    }
    private int [] dilatation () {
        return Arrays.copyOf(element,usSize*2);
    }
    private boolean isFull () {
        if(element.length==usSize) {
            return true;
        }
        return false;
    }

    @Override
    public void add(int pos, int data) {
        if(pos<0||pos>usSize) {
            throw new  PostException("post不合法");
        }
        if(isFull()) {
            element=dilatation();
        }
        int i=usSize-1;
        while(i>=pos) {
            element[i+1]=element[i];
            i--;
        }
        element[pos]=data;
        usSize++;
    }
    private  boolean isEmpty() {
        return element.length==0;
    }
    @Override
    public boolean contains(int toFind) {
        if(isEmpty()) {
            return false;
        }
        for (int i=0;i<usSize;i++) {
            if(element[i]==toFind) {
                return true;
            }

        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        if(isEmpty()) {
            return -1;
        }
        for (int i=0;i<usSize;i++) {
            if(element[i]==toFind) {
                return i;
            }

        }
        return -1;
    }

    @Override
    public int get(int pos) {
        if(pos<0||pos>usSize) {
            throw new  PostException("post不合法");
        }
        return element[pos];
    }

    @Override
    public void set(int pos, int value) {
        if(pos<0||pos>usSize) {
            throw new  PostException("post不合法");
        }
        element[pos]=value;

    }

    @Override
    public void remove(int toRemove) {
        int pos=indexOf(toRemove);
        if(pos==-1) {
            return;
        }
        while(pos<usSize-1) {
            element[pos]=element[pos+1];
            pos++;
            usSize--;
        }

    }

    @Override
    public int size() {
        return usSize;
    }

    @Override
    public void clear() {
        usSize=0;
    }

    @Override
    public void display() {
        System.out.print("[");
        for (int i = 0; i < usSize ; i++) {
            System.out.print(element[i]);
            if(i<usSize-1) {
                System.out.print(",");
            }

        }
        System.out.print("]");
    }


    @Override
    public int compareTo(E o) {
        return -1;
    }
}
